package com.iplant.basic.controller.bfc;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.UserGrads;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.SessionConstants;
import com.iplant.base.utils.log.TraceLoggerTool;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.DesUtil;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BFCService;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.basicenum.bfc.BFCClientType;
import com.iplant.basic.service.basicenum.bfc.BFCMessageStatus;
import com.iplant.basic.service.basicenum.bfc.BFCMessageType;
import com.iplant.basic.service.po.bfc.*;
import com.iplant.basic.service.po.bfc.auth.BFCModuleAuth;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSRoleItem;
import com.iplant.basic.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/api/HomePage")
public class HomePageController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(HomePageController.class);

    @Autowired
    BFCService wBFCService;

    @Autowired
    BMSService wBMSService;

    @ResponseBody
    @GetMapping("/Show")
    Object Show(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {


            BMSEmployee wBMSEmployee = GetSession(request);
            int wType = StringUtils.parseInt(request.getParameter("Type"));
            BFCHomeUser wHomeUser = new BFCHomeUser();

            wHomeUser.setName(wBMSEmployee.getName());
            /**
             * HomeUser的其他字段需要后续的xml补充
             */
            wHomeUser.setCompanyName(Constants.CompanyNameTitle);
            wHomeUser.setCompanyFaces(GetProjectName(request) + Constants.CompanyFaceUrl);
            wHomeUser.setFaces(wBMSEmployee.FaceIcon);
            wHomeUser.setLoginName(DesUtil.decrypt(wBMSEmployee.getLoginName(), SessionConstants.appSecret));
            wHomeUser.setName(wBMSEmployee.getName());
            wHomeUser.setGrad(wBMSEmployee.getGrad());
            wHomeUser.setLoginID(wBMSEmployee.getLoginID());
            wHomeUser.setUserID(wBMSEmployee.getID());
            wHomeUser.RoleList = wBMSService.BMS_GetFunctionListByLoginID(wBMSEmployee, wBMSEmployee.getID()).Result;

            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);


            Map<String, Object> Rst = new HashMap<String, Object>();

            ServiceResult<List<BFCHomePageGroup>> wBFCHomePageGroupList = wBFCService.BFC_GetHomePageGroupList(
                    wBMSEmployee, wProjectName, wType,  1, "", Pagination.getMaxSize());

            ServiceResult<List<BFCHomePageModule>> wBFCHomePageModuleList = wBFCService.BFC_GetHomePageModuleList(
                    wBMSEmployee, wProjectName, wType, wBMSEmployee.Grad==UserGrads.Default.getValue()?1:-1,
                    wBMSEmployee.Grad==UserGrads.Admin.getValue()?1:-1, wBMSEmployee.Grad==UserGrads.Engineer.getValue()?1:-1,
                    wBMSEmployee.Grad==UserGrads.System.getValue()?1:-1,1, "", -1, Pagination.getMaxSize());

            if (wBFCHomePageGroupList.Result.size() > 1) {
                wBFCHomePageGroupList.Result.sort(Comparator.comparing((BFCHomePageGroup p) -> p.OrderNum));
            }
            if (wBFCHomePageModuleList.Result.size() > 1) {
                wBFCHomePageModuleList.Result.sort(Comparator.comparing((BFCHomePageModule p) -> p.OrderNum));
            }

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1);
            List<BFCHomePageGroup> wServerRst = new ArrayList<BFCHomePageGroup>();

            Map<Integer, Integer> wEXCMessageDic = new HashMap<>();
            if (wType != BFCClientType.WEB.getValue()) {
                // GetMessageCount获取消息数量
                ServiceResult<Map<Integer, Integer>> wEXCMessageListResult = wBFCService.BFC_GetUndoMessagCount(
                        wBMSEmployee, wBMSEmployee.ID, -1);

                if (wEXCMessageListResult != null && wEXCMessageListResult.getResult() != null
                        && wEXCMessageListResult.getResult().size() > 0) {
                    wEXCMessageDic = wEXCMessageListResult.getResult();
                }
            }

            if (wEXCMessageDic.containsKey(0)) {
                wEXCMessageDic.remove(0);
            }
            // Map<Integer,EXCMessage>
            // wEXCMessageList.stream().collect(Collectors.toMap(p->p.ModuleID, k->k));

            // 获取这个人的所有权限

            List<Integer> wFunctionIDList = new ArrayList<Integer>();

            List<BMSRoleItem> wOwnFunctionList = wBMSService.BMS_GetFunctionListByLoginID(wBMSEmployee, wBMSEmployee.ID)
                    .getResult();
            if (wOwnFunctionList != null && wOwnFunctionList.size() > 0) {

                if (Constants.GetAuthRecord().ModuleList != null && Constants.GetAuthRecord().ModuleList.size() > 0)
                {
                    for (BMSRoleItem wBMSRoleItem : wOwnFunctionList)
                    {
                        for (BFCModuleAuth item : Constants.GetAuthRecord().ModuleList)
                        {
                            if (item.ExpireDate.compareTo(Calendar.getInstance()) < 0)
                                continue;

                            if (wBMSRoleItem.FunctionID != item.ModuleID)
                                continue;

                            wFunctionIDList.add(wBMSRoleItem.FunctionID);
                            break;
                        }
                    }
                }
                else {
                    wFunctionIDList = wOwnFunctionList.stream().map(p -> p.FunctionID).distinct().collect(
                            Collectors.toList());
                }

            }

            for (BFCHomePageGroup wBFCHomePageGroup : wBFCHomePageGroupList.getResult()) {
                if (wBFCHomePageGroup.getActive() != 1) continue;

                // 检查菜单权限
                if (wBFCHomePageGroup.getRoleID() > 0 && !wFunctionIDList.contains(wBFCHomePageGroup.getRoleID())) {
                    continue;
                }

                for (BFCHomePageModule wBFCHomePageModule : wBFCHomePageModuleList.getResult()) {
                    if (wBFCHomePageModule.getActive() != 1
                            || wBFCHomePageModule.getGroupID() != wBFCHomePageGroup.getID())
                        continue;

                    // 检查菜单权限
                    if (wBFCHomePageModule.getRoleID() > 0 && !wFunctionIDList.contains(
                            wBFCHomePageModule.getRoleID())) {
                        continue;
                    }

                    if (wType != BFCClientType.WEB.getValue()) {

                        if (wEXCMessageDic.containsKey(wBFCHomePageModule.getEventModule())) {
                            wBFCHomePageModule.MessageCount = wEXCMessageDic.get(wBFCHomePageModule.getEventModule());
                        }
                    }
                    wBFCHomePageModule.setSecretKey("");
                    wBFCHomePageGroup.getModuleList().add(wBFCHomePageModule);
                    wBFCHomePageGroup.MessageCount += wBFCHomePageModule.MessageCount;

                    SetMsgCount(wBFCHomePageGroupList.getResult(), wBFCHomePageGroup.GroupID,
                            wBFCHomePageModule.MessageCount);
                    // 找上级组 然后加

                }

                if (wBFCHomePageGroup.GroupID <= 0) {
                    // 主目录
                    wServerRst.add(wBFCHomePageGroup);
                } else if (wBFCHomePageGroup.ModuleList != null && wBFCHomePageGroup.ModuleList.size() > 0) {
                    // 此目录需要子项为
                    wServerRst.add(wBFCHomePageGroup);
                }
            }
            Rst.put("module", wServerRst);
            Rst.put("user", wHomeUser);
            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, Rst);

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_SUC, RetCode.SERVER_CODE_ERR_MSG);
            logger.error(e.toString());
        }
        return wResult;
    }

    private void SetMsgCount(List<BFCHomePageGroup> wBFCHomePageGroupList, int wGroupID, int wMsgCount) {

        try {
            if (wGroupID <= 0) return;
            Optional<BFCHomePageGroup> findFirst = wBFCHomePageGroupList.stream().filter(p -> p.ID == wGroupID)
                    .findFirst();
            if (!findFirst.isPresent()) {
                return;
            }

            BFCHomePageGroup wBFCHomePageGroup = findFirst.get();

            wBFCHomePageGroup.MessageCount += wMsgCount;
            if (wBFCHomePageGroup.GroupID <= 0) return;
            SetMsgCount(wBFCHomePageGroupList, wBFCHomePageGroup.GroupID, wMsgCount);
        } catch (Exception e) {
            logger.error("HomePage", "SetMsgCount", e.toString());
        }
    }

    @ResponseBody
    @GetMapping("/VersionLast")
    Object VersionLast(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            String client_info = StringUtils.parseString(request.getParameter("client_info"));
            if (client_info == null) client_info = "";

            BFCClientConfig wClientManage = Constants.getBFCClientConfig_APP();

            Map<String, Object> wInfo = new HashMap<String, Object>();
            wInfo.put("version_info", wClientManage.getVersionID());
            wInfo.put("description", wClientManage.getDescription());
            wInfo.put("url", GetProjectName(request) + wClientManage.getUrl());
            wInfo.put("is_update", wClientManage.isIsUpdate());

            if (wClientManage.isIsUpdate() && client_info.equalsIgnoreCase(wClientManage.getVersionID())) {
                wInfo.put("is_update", false);
            }

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wInfo);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/GroupAll")
    Object HomePageGroupAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            int wType = StringUtils.parseInt(request.getParameter("Type"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);
            String wName = StringUtils.parseString(request.getParameter("Name"));

            Pagination wPagination = GetPagination(request);
            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);
            ServiceResult<List<BFCHomePageGroup>> wServiceResult = wBFCService.BFC_GetHomePageGroupList(wBMSEmployee,
                    wProjectName, wType,  wActive, wName, wPagination);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(),
                        null);
            }

        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/ModuleAll")
    Object HomePageModuleAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            int wType = StringUtils.parseInt(request.getParameter("Type"),-1);
            int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

            int wUserEnable = StringUtils.parseInt(request.getParameter("UserEnable"),-1);
            int wAdminEnable = StringUtils.parseInt(request.getParameter("AdminEnable"),-1);
            int wEngineerEnable = StringUtils.parseInt(request.getParameter("EngineerEnable"),-1);
            int wSystemEnable = StringUtils.parseInt(request.getParameter("SystemEnable"),-1);
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wGroupID = StringUtils.parseInt(request.getParameter("GroupID"), -1);

            Pagination wPagination = GetPagination(request);
            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);
            ServiceResult<List<BFCHomePageModule>> wServiceResult = wBFCService.BFC_GetHomePageModuleList(wBMSEmployee,
                    wProjectName, wType, wUserEnable,wAdminEnable,wEngineerEnable,wSystemEnable, wActive, wName, wGroupID, wPagination);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {

                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(),
                        null);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/GroupUpdate")
    Object HomePageGroupUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }


            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);

            BFCHomePageGroup wBFCHomePageGroup = CloneTool.Clone(wParam.get("data"), BFCHomePageGroup.class);
            wBFCHomePageGroup.ProjectName = wProjectName;
            if (wBFCHomePageGroup.getID() <= 0) {
                wBFCHomePageGroup.setCreateTime(Calendar.getInstance());
                wBFCHomePageGroup.setCreatorID(wBMSEmployee.getID());
                wBFCHomePageGroup.setActive(0);
                if (StringUtils.isEmpty(wBFCHomePageGroup.Icon)) {
                    wBFCHomePageGroup.setIcon(
                            StringUtils.Format("{0}{1}", GetProjectName(request), Constants.MENU_GROUP_ICON));
                }
            } else if (wBFCHomePageGroup.getActive() >= 3 || wBFCHomePageGroup.getActive() < 0) {
                wBFCHomePageGroup.setActive(3);
            }
            wBFCHomePageGroup.setEditTime(Calendar.getInstance());
            wBFCHomePageGroup.setEditorID(wBMSEmployee.getID());

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_UpdateHomePageGroup(wBMSEmployee,
                    wBFCHomePageGroup);
            wBFCHomePageGroup.setID(wServiceResult.getResult());
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBFCHomePageGroup);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wBFCHomePageGroup);
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/GroupUpdateList")
    Object HomePageGroupUpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);
            List<BFCHomePageGroup> wBFCHomePageGroupList = CloneTool.CloneArray(wParam.get("data"),
                    BFCHomePageGroup.class);
            if (wBFCHomePageGroupList == null || wBFCHomePageGroupList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            for (BFCHomePageGroup wBFCHomePageGroup : wBFCHomePageGroupList) {
                wBFCHomePageGroup.ProjectName = wProjectName;
                if (wBFCHomePageGroup.getID() <= 0) {
                    wBFCHomePageGroup.setCreateTime(Calendar.getInstance());
                    wBFCHomePageGroup.setCreatorID(wBMSEmployee.getID());
                    wBFCHomePageGroup.setActive(0);
                    wBFCHomePageGroup.setIcon(
                            StringUtils.Format("{0}{1}", GetProjectName(request), Constants.MENU_GROUP_ICON));
                } else if (wBFCHomePageGroup.getActive() > 3 || wBFCHomePageGroup.getActive() < 0) {
                    wBFCHomePageGroup.setActive(3);
                }
                wBFCHomePageGroup.setEditTime(Calendar.getInstance());
                wBFCHomePageGroup.setEditorID(wBMSEmployee.getID());

                wServiceResult = wBFCService.BFC_UpdateHomePageGroup(wBMSEmployee,
                        wBFCHomePageGroup);
                wBFCHomePageGroup.setID(wServiceResult.getResult());
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null,
                        wServiceResult.Result);
            }
        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/ModuleUpdate")
    Object HomePageModuleUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);

            BFCHomePageModule wBFCHomePageModule = CloneTool.Clone(wParam.get("data"), BFCHomePageModule.class);
            wBFCHomePageModule.ProjectName = wProjectName;
            if (wBFCHomePageModule.getID() <= 0) {
                wBFCHomePageModule.setCreateTime(Calendar.getInstance());
                wBFCHomePageModule.setCreatorID(wBMSEmployee.getID());
                wBFCHomePageModule.setActive(0);
                if (StringUtils.isEmpty(wBFCHomePageModule.Icon)) {
                    wBFCHomePageModule.setIcon(
                            StringUtils.Format("{0}{1}", GetProjectName(request), Constants.MENU_MODULE_ICON));
                }
            } else if (wBFCHomePageModule.getActive() > 3 || wBFCHomePageModule.getActive() < 0) {
                wBFCHomePageModule.setActive(3);
            }
            wBFCHomePageModule.setEditTime(Calendar.getInstance());
            wBFCHomePageModule.setEditorID(wBMSEmployee.getID());

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_UpdateHomePageModule(wBMSEmployee,
                    wBFCHomePageModule);
            wBFCHomePageModule.setID(wServiceResult.getResult());
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBFCHomePageModule);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wBFCHomePageModule);
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;

    }

    @ResponseBody
    @PostMapping("/ModuleUpdateList")
    Object ModuleUpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (wBMSEmployee.Grad < UserGrads.Engineer.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
                return wResult;
            }
            List<BFCHomePageModule> wBFCHomePageModuleList = CloneTool.CloneArray(wParam.get("data"),
                    BFCHomePageModule.class);

            String wProjectName = GetRealProjectName(request);
            if (wProjectName.startsWith("/"))
                wProjectName = wProjectName.substring(1);

            if (wBFCHomePageModuleList == null || wBFCHomePageModuleList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = new ServiceResult<>();
            for (BFCHomePageModule wBFCHomePageModule : wBFCHomePageModuleList) {
                wBFCHomePageModule.ProjectName = wProjectName;
                if (wBFCHomePageModule.getID() <= 0) {
                    wBFCHomePageModule.setCreateTime(Calendar.getInstance());
                    wBFCHomePageModule.setCreatorID(wBMSEmployee.getID());
                    wBFCHomePageModule.setActive(0);
                    wBFCHomePageModule.setIcon(
                            StringUtils.Format("{0}{1}", GetProjectName(request), Constants.MENU_MODULE_ICON));
                } else if (wBFCHomePageModule.getActive() > 3 || wBFCHomePageModule.getActive() < 0) {
                    wBFCHomePageModule.setActive(3);
                }
                wBFCHomePageModule.setEditTime(Calendar.getInstance());
                wBFCHomePageModule.setEditorID(wBMSEmployee.getID());

                wServiceResult = wBFCService.BFC_UpdateHomePageModule(wBMSEmployee,
                        wBFCHomePageModule);
                wBFCHomePageModule.setID(wServiceResult.getResult());
            }


            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null,
                        wServiceResult.Result);
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;

    }

    @GetMapping("/Index")
    String Index() {

        return "redirect:/index.html";
    }

    @ResponseBody
    @GetMapping("/MessageBell")
    Object MessageBell(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wMessageType = StringUtils.parseInt(request.getParameter("MessageType"));

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));

            List<Integer> wMessageIDList = StringUtils.parseIntList(
                    StringUtils.parseString(request.getParameter("EventModules")), ",");

            if (wResponsorID <= 0) wResponsorID = wBMSEmployee.ID;

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_GetMessageCount(wBMSEmployee, wResponsorID,
                    wMessageType, wMessageIDList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);

            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
            logger.error(e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/ModuleMessageCount")
    Object ModuleMessageCount(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            ServiceResult<Map<Integer, Integer>> wServiceResult = wBFCService.BFC_GetUndoMessagCount(wBMSEmployee,
                    wBMSEmployee.getID(), -1);

            Map<String, Integer> wServiceRst = new HashMap<String, Integer>();
            if (wServiceResult.getResult() != null && wServiceResult.getResult().size() > 0) {
                for (Integer wInteger : wServiceResult.getResult().keySet()) {
                    wServiceRst.put(wInteger + "", wServiceResult.getResult().get(wInteger));
                }
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
            logger.error(e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/MsgAll")
    Object BFCMessageAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));
            List<Integer> wModuleID = StringUtils.parseIntList(request.getParameter("ModuleID"), ",");
            List<Integer> wMessageIDList = StringUtils.parseIntList(request.getParameter("MessageID"), ",");
            int wType = StringUtils.parseInt(request.getParameter("Type"));
            int wShiftID = StringUtils.parseInt(request.getParameter("ShiftID"));
            List<Integer> wActive = StringUtils.parseIntList(request.getParameter("Active"), ",");
            int wStepID = StringUtils.parseInt(request.getParameter("StepID"));
            int wSendStatus = StringUtils.parseInt(request.getParameter("SendStatus"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BFCMessage>> wServiceResult = wBFCService.BFC_GetMessageList(wBMSEmployee, wResponsorID,
                    wType, wModuleID, wMessageIDList, wActive, wSendStatus, wShiftID, wStartTime, wEndTime, wStepID,
                    wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(),
                        "");
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgUpdate")
    Object BFCMessageUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<BFCMessage> wBFCMessageList = CloneTool.CloneArray(wParam.get("data"), BFCMessage.class);

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_UpdateMessageList(wBMSEmployee, wBFCMessageList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wBFCMessageList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wBFCMessageList, null);
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgSend")
    Object BFCMessageSend(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data") || !wParam.containsKey("SendStatus")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wSendStatus = StringUtils.parseInt(wParam.get("SendStatus"));
            List<BFCMessage> wBFCMessageList = CloneTool.CloneArray(wParam.get("data"), BFCMessage.class);

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_SendMessageList(wBMSEmployee, wBFCMessageList,
                    wSendStatus);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wBFCMessageList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wBFCMessageList, null);
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgReceive")
    Object BFCMessageReceive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("MessageIDList") || !wParam.containsKey("ModuleID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));
            int wModuleID = StringUtils.parseInt(wParam.get("ModuleID"));
            List<Integer> wMessageIDList = CloneTool.CloneArray(wParam.get("MessageIDList"), Integer.class);

            int wResponsorID = wParam.containsKey("ResponsorID") ? StringUtils.parseInt(wParam.get("ResponsorID")) :
                    wBMSEmployee.getID();

            List<Integer> wStepID = wParam.containsKey("StepID") ? CloneTool.CloneArray(
                    wParam.get("StepID"), Integer.class) : new ArrayList<Integer>();

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_ReceiveMessage(wBMSEmployee, wResponsorID,
                    wMessageIDList, wStepID, wModuleID,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgRead")
    Object BFCMessageRead(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("MsgIDList")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));
            List<Integer> wMsgIDList = CloneTool.CloneArray(wParam.get("MsgIDList"), Integer.class);

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_HandleMessageByIDList(wBMSEmployee, wMsgIDList,
                    BFCMessageStatus.Read.getValue(), -1,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgSent")
    Object BFCMessageSent(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("MsgIDList")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<Integer> wMsgIDList = CloneTool.CloneArray(wParam.get("MsgIDList"), Integer.class);

            int wSendStatus = StringUtils.parseInt(wParam.get("SendStatus"));


            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_HandleMessageByIDList(wBMSEmployee, wMsgIDList,
                    BFCMessageStatus.Sent.getValue(), wSendStatus,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgHandle")
    Object BFCMessageHandle(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("MessageIDList") || !wParam.containsKey("ModuleID") || !wParam.containsKey(
                    "Status")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wModuleID = StringUtils.parseInt(wParam.get("ModuleID"));
            int wStatus = StringUtils.parseInt(wParam.get("Status"));
            List<Integer> wStepID = wParam.containsKey("StepID") ? CloneTool.CloneArray(
                    wParam.get("StepID"), Integer.class) : new ArrayList<>();

            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));
            List<Integer> wMessageIDList = CloneTool.CloneArray(wParam.get("MessageIDList"), Integer.class);

            int wResponsorID = wParam.containsKey("ResponsorID") ? StringUtils.parseInt(wParam.get("ResponsorID")) :
                    wBMSEmployee.getID();

            int wIsAuto = wParam.containsKey("IsAuto") ? StringUtils.parseInt(wParam.get("IsAuto")) :
                    wBMSEmployee.getID();

            if (wStatus != BFCMessageStatus.Close.getValue() && wStatus != BFCMessageStatus.Finished.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            if (wResponsorID == 0) {
                wResponsorID = wBMSEmployee.getID();
            }

            if (wStatus == BFCMessageStatus.Finished.getValue() && wIsAuto == 1 &&
                    (wResponsorID > 0 || wResponsorID == -100)) {
                wBFCService.BFC_HandleMessage(wBMSEmployee, -1, wMessageIDList, wStepID, wModuleID,
                        BFCMessageType.Task.getValue(), BFCMessageStatus.Close.getValue(),wCreateTime,Calendar.getInstance());
            }
            ServiceResult<Integer> wServiceResult = wBFCService.BFC_HandleMessage(wBMSEmployee, wResponsorID,
                    wMessageIDList, wStepID, wModuleID, BFCMessageType.Task.getValue(), wStatus,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgHandleTask")
    Object MsgHandleTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if ((!wParam.containsKey("TaskIDList") && !wParam.containsKey("MessageIDList")) || !wParam.containsKey(
                    "ModuleID") || !wParam.containsKey("Status") ||
                    !wParam.containsKey("StepID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wModuleID = StringUtils.parseInt(wParam.get("ModuleID"));
            int wStepID = StringUtils.parseInt(wParam.get("StepID"));
            int wStatus = StringUtils.parseInt(wParam.get("Status"));
            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));

            List<Integer> wTaskIDList = (wParam.containsKey("TaskIDList")) ? CloneTool.CloneArray(wParam.get(
                    "TaskIDList"), Integer.class)
                    : CloneTool.CloneArray(wParam.get("MessageIDList"), Integer.class);

            int wResponsorID = wParam.containsKey("ResponsorID") ? StringUtils.parseInt(wParam.get("ResponsorID")) :
                    wBMSEmployee.getID();

            int wIsAuto = wParam.containsKey("IsAuto") ? StringUtils.parseInt(wParam.get("IsAuto")) : 0;

            if (wStatus != BFCMessageStatus.Close.getValue() && wStatus != BFCMessageStatus.Finished.getValue()) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_HandleTaskMessage(wBMSEmployee, wResponsorID,
                    wTaskIDList, wModuleID, wStepID, wStatus, wIsAuto,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/MsgForward")
    Object MsgForward(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("MessageID") || !wParam.containsKey("ModuleID") || !wParam.containsKey("StepID") ||
                    !wParam.containsKey("ResponsorID") || !wParam.containsKey("ForwarderList")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wModuleID = StringUtils.parseInt(wParam.get("ModuleID"));
            int wMessageID = StringUtils.parseInt(wParam.get("MessageID"));
            int wStepID = StringUtils.parseInt(wParam.get("StepID"));
            int wResponsorID = StringUtils.parseInt(wParam.get("ResponsorID"));
            List<Integer> wForwarderList = CloneTool.CloneArray(wParam.get("ForwarderList"), Integer.class);
            if (wModuleID <= 0 || wMessageID <= 0 || wStepID <= 0 || (wResponsorID <= 0 && wResponsorID != -100) ||
                    wForwarderList == null || wForwarderList.size() <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wBFCService.BFC_ForwardMessage(wBMSEmployee, wResponsorID,
                    wForwarderList, wModuleID, wMessageID, wStepID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/GetApiLogStatistics")
    Object GetApiLogStatistics(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1);

            String wProjectName = StringUtils.parseString(request.getParameter("ProjectName"));
            String wUri = StringUtils.parseString(request.getParameter("Uri"));
            String wURIName = StringUtils.parseString(request.getParameter("URIName"));
            String wMethod = StringUtils.parseString(request.getParameter("Method"));
            int wRequestTimes = StringUtils.parseInt(request.getParameter("RequestTimes"));

            int wAvgIntervalTime = StringUtils.parseInt(request.getParameter("AvgIntervalTime"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<Map<String, Object>>> wServiceResult = TraceLoggerTool.GetApiLogStatistics(wLoginUser,
                    wStartTime, wEndTime, wAvgIntervalTime, wProjectName, wUri, wURIName, wMethod, wRequestTimes,
                    wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/GetApiLog")
    Object GetApiLog(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1);

            String wProjectName = StringUtils.parseString(request.getParameter("ProjectName"));
            String wUri = StringUtils.parseString(request.getParameter("Uri"));
            String wURIName = StringUtils.parseString(request.getParameter("URIName"));
            String wMethod = StringUtils.parseString(request.getParameter("Method"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wUserID = StringUtils.parseInt(request.getParameter("UserID"));
            int wUserControl = StringUtils.parseInt(request.getParameter("UserControl"));

            int wIntervalMin = StringUtils.parseInt(request.getParameter("IntervalMin"));
            int wIntervalMax = StringUtils.parseInt(request.getParameter("IntervalMax"));
            int wResponseStatus = StringUtils.parseInt(request.getParameter("ResponseStatus"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<Map<String, Object>>> wServiceResult = TraceLoggerTool.GetApiLog(wBMSEmployee, wUserID,
                    wProjectName, wUri, wURIName, wMethod, wUserControl, wIntervalMin, wIntervalMax, wResponseStatus,
                    wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString());
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/SetApiControl")
    Object SetApiControl(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("ProjectName") || !wParam.containsKey("Uri") || !wParam.containsKey("URIName") ||
                    !wParam.containsKey("UserControl")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            String wProjectName = StringUtils.parseString(wParam.get("ProjectName"));
            String wUri = StringUtils.parseString(wParam.get("Uri"));
            String wURIName = StringUtils.parseString(wParam.get("URIName"));
            int wUserControl = StringUtils.parseInt(wParam.get("UserControl"));
            if (StringUtils.isEmpty(wProjectName) || StringUtils.isEmpty(wUri)) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = TraceLoggerTool.SetApiControl(wBMSEmployee, wProjectName, wUri,
                    wURIName, wUserControl);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取已做集合 HasDoList集合 HasDoCounts消息数
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/GetHasDoList")
    Object GetHasDoList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));

            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            List<Integer> wEventModuleList = StringUtils.parseIntList(
                    StringUtils.parseString(request.getParameter("EventModules")).split(","));
            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));

            if (wResponsorID <= 0) {
                wResponsorID = wBMSEmployee.getID();
            }

            if (wEventModuleList != null) wEventModuleList.removeIf(p -> p <= 0);

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<BFCMessage>> wServiceResult = wBFCService.BFC_GetMessageDoneList(wBMSEmployee,
                    wResponsorID, wEventModuleList, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {

                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString());
        }
        return wResult;
    }

    /**
     * 获取待做集合 HasDoList集合 HasDoCounts消息数
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/GetUnDoList")
    Object GetUnDoList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"),-1);
            int wModuleID = StringUtils.parseInt(request.getParameter("ModuleID"),-1);
            List<Integer> wEventModuleList = StringUtils.parseIntList(
                    StringUtils.parseString(request.getParameter("EventModules")).split(","));
            int wMessageID = StringUtils.parseInt(request.getParameter("MessageID"),-1);
            int wShiftID = StringUtils.parseInt(request.getParameter("ShiftID"),-1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            if (wResponsorID <= 0) {
                wResponsorID = wBMSEmployee.getID();
            }

            if (wEventModuleList != null) wEventModuleList.removeIf(p -> p <= 0);

            if (wModuleID > 0 && !wEventModuleList.contains(wModuleID)) wEventModuleList.add(wModuleID);

            ServiceResult<List<BFCMessage>> wServiceResult = wBFCService.BFC_GetUndoMessageList(wBMSEmployee,
                    wResponsorID, wEventModuleList, wMessageID, wShiftID, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {

                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
                SetResult(wResult, "MsgCount", wPagination.getTotalCount());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString());
        }
        return wResult;
    }

    /**
     * 获取通知集合
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/GetNoticeList")
    Object GetNoticeList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));

            int wUseTime = StringUtils.parseInt(request.getParameter("UseTime"));

            List<Integer> wEventModuleList = StringUtils.parseIntList(
                    StringUtils.parseString(request.getParameter("EventModules")).split(","));
            int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));

            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            if (wResponsorID <= 0) {
                wResponsorID = wBMSEmployee.getID();
            }

            if (wEventModuleList != null) wEventModuleList.removeIf(p -> p <= 0);

            Pagination wPagination = GetPagination(request);

            OutResult<Integer> wCount = new OutResult<Integer>(0);
            ServiceResult<List<BFCMessage>> wServiceResult = wBFCService.BFC_GetNoticeList(wBMSEmployee, wResponsorID,
                    wEventModuleList, wActive, wUseTime, wStartTime, wEndTime, wCount, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {

                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());

                SetResult(wResult, "MsgCount", wCount.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            logger.error(e.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString());
        }
        return wResult;
    }


    @ResponseBody
    @GetMapping("/TaskAll")
    Object TaskAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wResponsorID = StringUtils.parseInt(request.getParameter("ResponsorID"));
            List<Integer> wModuleID = StringUtils.parseIntList(request.getParameter("ModuleID"), ",");
            int wTagType = StringUtils.parseInt(request.getParameter("TagType"));
            String wIndexName = StringUtils.parseString(request.getParameter("IndexName"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<? extends BasePO>> wServiceResult = wBFCService.BFC_GetTaskList(
                    wBMSEmployee, wTagType, wIndexName, wResponsorID, wModuleID, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
                        wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(),
                        "");
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @GetMapping("/TaskInfo")
    Object TaskInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wTaskID = StringUtils.parseInt(request.getParameter("TaskID"));
            String wIndexName = StringUtils.parseString(request.getParameter("IndexName"));
            String wTaskCode = StringUtils.parseString(request.getParameter("TaskCode"));


            ServiceResult<BasePO> wServiceResult = wBFCService.BFC_GetTaskInfo(
                    wBMSEmployee, wIndexName, wTaskID, wTaskCode);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(),
                        "");
            }

        } catch (Exception e) {
            logger.error(e.toString());

            wResult = GetResult(RetCode.SERVER_CODE_ERR, e.toString(), null, null);
        }
        return wResult;
    }

    @ResponseBody
    @PostMapping("/TaskUpdate")
    public Object TaskUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);
            if (!wParam.containsKey("data") || !wParam.containsKey("IndexName") || !wParam.containsKey("ModuleID")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            int wModuleID = StringUtils.parseInt(wParam.get("ModuleID"));
            String wIndexName = StringUtils.parseString(wParam.get("IndexName"));
            int wStepID = StringUtils.parseInt(wParam.getOrDefault("StepID", 0));
            int wStatus =StringUtils.parseInt(wParam.getOrDefault("Status", 0));


            Calendar wCreateTime = StringUtils.parseCalendar(wParam.getOrDefault("CreateTime", ""));

            // 获取参数
            List<Map> wTaskList = CloneTool.CloneArray(wParam.get("data"), Map.class);

            if (wTaskList == null || wTaskList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<? extends BasePO>> wServiceResult = wBFCService.BFC_UpdateTaskList(
                    wLoginUser, wIndexName, wTaskList, wModuleID, wStepID, wStatus,wCreateTime,Calendar.getInstance());

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 复制菜单子项
     */
//    @GetMapping("/CopyMenuItem")
//    public Object CopyMenuItem(HttpServletRequest request) {
//        Object wResult = new Object();
//        try {
//            BMSEmployee wLoginUser = GetSession(request);
//
//            int wMenuItemID = StringUtils.parseInt(request.getParameter("MenuItemID"));
//            int wTargetGrad = StringUtils.parseInt(request.getParameter("TargetGrad"));
//            int wTargetGroupID = StringUtils.parseInt(request.getParameter("TargetGroupID"));
//
//            if (wMenuItemID <= 0 || wTargetGrad <= 0 || wTargetGroupID <= 0) {
//                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
//            }
//
//            ServiceResult<Integer> wServiceResult = wBFCService.BFC_CopyMenuItem(wLoginUser, wMenuItemID, wTargetGrad
//                    , wTargetGroupID);
//
//            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
//                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
//            } else {
//                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
//            }
//        } catch (Exception ex) {
//            logger.error(ex.toString());
//            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
//        }
//        return wResult;
//    }
}
